package com.spider.silence.utils;

import com.spider.silence.constant.Constant;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by qiuhao on 2017/5/3.
 */
public class FileUtils {

    public static final String temp= Constant.FILE_TMP_FOLDER;//临时文件存放目录

    /**
     * 帖子每页的临时文件
     * @param name 帖子每页的id    ： 帖子ID-pageNo-n
     * @param content 每一页帖子的内容
     * @return
     */
    public synchronized static File createTempFile(String name,String content){
        if(!"".equals(name) && name!=null && !"".equals(content) && content!=null){
            try {
                File f=new File(temp);
                if(!f.exists()){
                    f.mkdirs();
                }
               File file= File.createTempFile(name,".txt",f);
               FileWriter fw=new FileWriter(file,true);
               BufferedWriter bw = new BufferedWriter(fw);


               bw.write(content);
               bw.close();
               return file;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    /**
     * 过滤掉帖子内容中的所有html标签
     * @param content
     * @return
     */
    public static String filterImg(String content){
        // 过滤文章内容中的html
        content = content.replaceAll("</?[^<]+>", "");
        // 去除字符串中的空格 回车 换行符 制表符 等
//        content = content.replaceAll("\\s*|\t|\r|\n", "");
        // 去除空格
        content = content.replaceAll("&nbsp;", "");

        return content;
    }

    /**
     * 过滤出一段字符串中<img>标签中src的值</>
     * @param content
     * @return
     */
    public static List<String> filterImgSrc(String content){
        if(content==null || "".equals(content)){
            return null;
        }
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<String>();
//       String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(content);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }

    /**
     * 过滤出a标签中的href值
     * @param content
     * @return
     */
    public static String getHref(String content){
        Pattern p = Pattern.compile(".*href=\\\'(.*?)\\\'");

        Matcher m = p.matcher(content);

        if (m.find()) {
            content = m.group(1);
        }
        return content;
    }

    /**
     * 过滤出<body></body>中的内容
     * @param content
     * @return
     */
    public static String getBody(String content){
        Pattern p = Pattern.compile("<body>(.*?)</body>");
        Matcher m = p.matcher(content);

        if (m.find()) {
            content = m.group(1);
        }

        return content;
    }
}
